<?php
class Article extends CI_Controller{

	public function __construct(){
		parent::__construct();
		$this->load->model("article_model",'article');
		$this->load->model("category_model",'category');
		
	}
	
    //新加
	public function add(){
		$this->load->helper('form');
		$this->load->library("form_validation");

		$this->form_validation->set_rules('title', 'title', 'required');
  		$this->form_validation->set_rules('text', 'text', 'required');

  		if($this->form_validation->run() === false){
			$this->load->view("templates/header");
  			$this->load->view("admin/article/add");
			$this->load->view("templates/footer");
  		}else{
  			if($this->article->set_article()){
				$this->lists();
			}
  		}
		
	}

	//更新
	public function edit(){

		$this->load->helper('form','url');
		
		if(isset($_POST['action'])){
			$action = $_POST['action'];
			
			//文章整体更新
			if($action == 'update'){
				$data = array(
					'title' => $_POST['title'],
					'category_id' => $_POST['category_id'],
					'text' => $_POST['text'],
					'status' => $_POST['status']
				);
				
				$where = array(
					'id' => $_POST['id']
				);

				$up = $this->article->up_article($where,$data);

				if($up){
					//$this->load->view('admin/article/edit',array('result' => "修改成功"));
					$this->lists();
				}
			}
			
			
			
		}else{
			
			$id = $this->uri->segment(3);//取得id
			$articles = $this->article->get_article($id);
	

  			$data = array(
					'articles' => $articles,
  					'category_id' =>  explode(',',$articles['category_id']),
					'cates' => $this->category->getAllCategory2()
  			);
			
			
			$this->load->view("templates/header");
			$this->load->view('admin/article/edit',$data);
			$this->load->view("templates/footer");
		}

	}
	
	//更新状态
	public function status(){
		//文章状态更新
			
				$data = array(
					'status' => $_POST['status']
				);
				
				$where = array(
					'id' => $_POST['id']
					//'id' => explode(',',$_POST['id'])
				);
				
				
				$up = $this->article->up_status($where,$data);
				if($up){
					$json['error'] = 0;
					$json['content'] = $_POST['status'];
					echo json_encode($json);
					return ;
					
				}else{
					$json['error'] = 1;
					$json['content'] = $_POST['status'];
					echo json_encode($json);
					return ;
				}
			
			
	} 

	//删除
	public function delete(){
		$id = $this->uri->segment(3);
		if($row = $this->article->delete($_POST['id'])){
			$json['error'] = 0;
			$json['content'] = '';
			echo json_encode($json);
			return ;
		}else{
			$json['error'] = 1;
			$json['content'] = '删除失败!';
			echo json_encode($json);
			return ;
		}
	
	}
	
	//全部列出
	public function lists(){
      	$this->load->library('pagination');

      	$config['base_url'] = site_url("article/lists");

		$config['total_rows'] = $this->db->count_all('article');

		$config['per_page'] = 10;
		
		//$config['page_query_string'] = TRUE;
		
		//$page = (int)$this->uri->segment('per_page');
		$page = (int)$this->uri->segment(3);
		
		$this->pagination->initialize($config);

		$data = array(
             "articles" => $this->article->articleList($config['per_page'],$page)
		
        );
		
		if($category_id = (int)$this->uri->segment(4)){
			$data = array(
				'articles' => $this->article->articleList($config['per_page'],$page,$category_id)
			);
		}
		//echo $this->db->last_query();
		$this->load->view("templates/header");
		$this->load->view("admin/article/lists",$data);
		$this->load->view("templates/footer");
		
	}



}